package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class GroupUploadSpotPeer extends BasePeer{
	public static final String tableName = "_Group_UploadSpot";
	public static final String GROUPID = "groupId";
	public static final String SPOTID = "spotId";
	public static final String[] columnNames=new String[]{"groupId","spotId"};
	public static GroupUploadSpot getGroupUploadSpotByPKs(Object groupId,Object spotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(GroupUploadSpotPeer.tableName,GroupUploadSpotPeer.columnNames," TRUE  AND `groupId`=?  AND `spotId`=? ",new Object[]{groupId,spotId,},0, 1,conn);
			GroupUploadSpot groupUploadSpot = new GroupUploadSpot();
		fill(rs,groupUploadSpot);
		return groupUploadSpot;
	}
	public static GroupUploadSpot getGroupUploadSpotReferenceColumnsOnlyByPKs(Object groupId,Object spotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(GroupUploadSpotPeer.tableName,new String[]{"groupId","spotId",}," TRUE AND `groupId`=?AND `spotId`=?",new Object[]{groupId,spotId,}, 0,  1,conn);
			GroupUploadSpot groupUploadSpot = new GroupUploadSpot();
		fill(rs,groupUploadSpot);
		return groupUploadSpot;
	}
	public static GroupUploadSpot getGroupUploadSpotByGroupId(long groupId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(GroupUploadSpotPeer.tableName,GroupUploadSpotPeer.columnNames,"`groupId`=?",new Object[]{groupId},0, 1,conn);
			GroupUploadSpot groupUploadSpot = new GroupUploadSpot();
		fill(rs,groupUploadSpot);
		return groupUploadSpot;
	}
	public static GroupUploadSpot getGroupUploadSpotBySpotId(long spotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(GroupUploadSpotPeer.tableName,GroupUploadSpotPeer.columnNames,"`spotId`=?",new Object[]{spotId},0, 1,conn);
			GroupUploadSpot groupUploadSpot = new GroupUploadSpot();
		fill(rs,groupUploadSpot);
		return groupUploadSpot;
	}
	public static void fill(ResultSet rs, GroupUploadSpot groupUploadSpot) throws SQLException {
		if(rs.next()){
			try{groupUploadSpot.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{groupUploadSpot.setSpotId(rs.getLong("spotId"));}catch(Exception ex){}
			groupUploadSpot.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<GroupUploadSpot> groupUploadSpotCollection) throws SQLException {
		while(rs.next()){
			GroupUploadSpot groupUploadSpot=new GroupUploadSpot();
			try{groupUploadSpot.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{groupUploadSpot.setSpotId(rs.getLong("spotId"));}catch(Exception ex){}
			groupUploadSpot.resetModified();
			groupUploadSpotCollection.add(groupUploadSpot);
		}
	}
	public static boolean update(GroupUploadSpot groupUploadSpot, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < GroupUploadSpotPeer.columnNames.length; i++) {
			if (((GroupUploadSpotBase)groupUploadSpot).modified[i]) {
				columns.add(GroupUploadSpotPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_Group_UploadSpot` SET ");
		sql.append("WHERE TRUE AND `groupId` = ? AND `spotId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,groupUploadSpot.get(column));
		}
		pstmt.setObject(columns.size()+1+0,groupUploadSpot.get("groupId"));
		pstmt.setObject(columns.size()+1+1,groupUploadSpot.get("spotId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(GroupUploadSpot groupUploadSpot, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (groupUploadSpot.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_Group_UploadSpot` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (groupUploadSpot.get(column) != null) {
				pstmt.setObject(i + 1 + j, groupUploadSpot.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<GroupUploadSpot> getGroupUploadSpotCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<GroupUploadSpot> collection=new Vector<GroupUploadSpot>();
		fill(rs,collection);
		return collection;
	}
}
